package questionaire;
import java.io.*;
import java.util.*;

/**
 *
 * @author balbir kaur
 */
public class QuestionsRead {

    Properties p=new Properties();
    String fs=null; // file seperator os dependent
    String questionfilename;
    String homedir;
    HashMap qanda=null;
    HashMap qandcorrectanswer=null;
    int questionNo;
    private String tweetStart=null;
    private String tweetEnd=null;
    QuestionsRead(){
    this.setPathToFile();
    this.readPropertyFile();
    this.readquestions();
   // this.checkHashValues(); //used to confirm that hash values are correct uncomment
    //to check whether this holds true for your file
    }
    /* reads filename from propertyfile*/
    public void readPropertyFile(){
    FileInputStream propfile=null;
        try{
        propfile=new FileInputStream(homedir+fs+"quiz.properties");
        p.load(propfile);
        /* gets the name of file containing the questions */
        questionfilename=p.getProperty("filename");
        questionNo=Integer.parseInt(p.getProperty("questions", "10"));
        tweetStart=p.getProperty("tweetstart","#csquiz I scored ");
        tweetEnd=p.getProperty("tweetend"," Take the quiz! http://ramannanda.blogdns.com:4000/cs1.6quiz");
        propfile.close();
    }
    catch(Exception e){
    System.out.println("Error the file does not exist"+e.getMessage());
    }
    finally{
        if(propfile!=null){
            try{
            propfile.close();}
            catch(Exception e){
            }
        }
    }
    }
    public int getQuestionNo(){
    return(this.questionNo);
    }

    public void readquestions(){
        String temp=null;
        try{
        FileReader fr=new FileReader(homedir+fs+questionfilename);
        BufferedReader br=new BufferedReader(fr,2048);
        StringBuffer buffer=new StringBuffer("");
        String s;
        while((s=br.readLine())!=null){
        buffer.append(s);
        }
        br.close();
        fr.close();
         temp=new String(buffer);
       }
        catch(Exception e){
        System.out.println("The questions file does not exist "+e.getMessage());
        }
        String op[]=temp.split("#");
        String correctanswer=null;
        String answers[]=null;
        int index=0;
        String question=null;
        qanda=new HashMap();
        qandcorrectanswer=new HashMap();
        boolean flag=false;
        for(int i=0;i<op.length;i++){
            if(flag){
            answers=op[i].split(",");
            for(int j=0;j<answers.length;j++){
            if((index=answers[j].lastIndexOf("@"))!=-1){
            answers[j]=answers[j].substring(0, index);
            correctanswer=answers[j];
            }
            
            }
            qanda.put(question, answers);
            qandcorrectanswer.put(question, correctanswer);
            flag=false;
            }
            else
            {question=op[i];
             flag=true;
        }
        }
    }

    
    //Sets the file seperator for an os and also the home dir
    public void setPathToFile(){
       fs=System.getProperty("file.separator");
       homedir=System.getProperty("user.home");
       
    }
    public void checkHashValues(){
    Set<Map.Entry<String,String>> set=qandcorrectanswer.entrySet();
    for(Map.Entry<String,String>me:set){
    System.out.println(me.getKey()+":"+me.getValue());
        }
    }
    public HashMap getQuestionAndAnswers(){
    return(this.qanda);
    }
    public HashMap getQuestionAndCorrectAnswer(){
    return(this.qandcorrectanswer);
    }
    public String getTweetEnd(){
    return this.tweetEnd;
    }
    public String getTweetStart(){
        return this.tweetStart;
    }

    /*public static void main(String args[]){
        QuestionsRead r=new QuestionsRead();


    }
     */

}
